﻿using AutoMapper;
using Customer.API.Applcation.Command.MeetCommand;
using Customer.Domain;
using Customer.Infrstructre.Meet;
using MediatR;


namespace Customer.API.Applcation.CommandHandlers.MeetCommandHandler
{
    /// <summary>
    /// 新增应付中介者
    /// </summary>
    public class ADDMeetCommandHandler:IRequestHandler<ADDMeetCommand,int>
    {
        private readonly IAccountsRepository _accountRep;
        private readonly IMapper _mapper;

        public ADDMeetCommandHandler(IAccountsRepository accountRep, IMapper mapper)
        {
            _accountRep = accountRep;
            _mapper = mapper;
        }

        public async Task<int> Handle(ADDMeetCommand request, CancellationToken cancellationToken)
        {
            var res = (await _accountRep.GetAllAsync()).Where(x => x.AccountsName == request.addDTO.AccountsName);
            if(res.Count() > 0)
            {
                return -1;//名称重复
            }
            return await _accountRep.AddAsync(_mapper.Map<Accounts>(request.addDTO));
        }
    }
}
